<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-form-item label="设备类型名称">
          <el-input
            v-model="queryParams.typeName"
            placeholder="请输入设备类型名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="addDeviceTypes"
            >新增</el-button
          >
        </el-col>

        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="postList"
        stripe
        border
        max-height="500"
      >
        <el-table-column label="设备类型名称" align="center" prop="typeName" />
        <el-table-column label="所属品类" align="center">
          <template #default="scope">
            <span>
              {{
                device_category.find(
                  (item) => item.value == scope.row.categoryId
                )
                  ? device_category.find(
                      (item) => item.value == scope.row.categoryId
                    ).label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="备注" align="center" prop="notes" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="edit(scope.row)"
              >修改</el-button
            >
            <el-button
              link
              type="danger"
              icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      v-model="open"
      :title="title"
      append-to-body
      width="600px"
      v-if="open"
      @close="cancel"
      :close-on-click-modal="false"
    >
      <el-form
        ref="deviceTypeRef"
        :model="form"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item label="所属大类" prop="categoryId">
          <el-select
            v-model="form.categoryId"
            clearable
            placeholder="请选择所属大类"
            style="width: 100%"
          >
            <el-option
              v-for="dict in device_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入设备类型名称" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input
            v-model="form.notes"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 8 }"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormDebounceHandler"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="deviceType">
import {
  addelderArchive,
  elderArchiveDel,
  updateelderArchive,
} from "@/api/record/api";

const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, device_category } = proxy.useDict(
  "sys_normal_disable",
  "sys_user_sex",
  "device_category"
);
import {
  devicetypeList,
  devicetypeDel,
  adddevicetype,
  updatedevicetype,
  adddevicereg,
} from "@/api/equipment/index";

import { debounce } from "@/utils/debounce.js";
import { convertISOToFormattedDate } from "@/utils/index";
const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const title = ref("新增设备类型");

const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    typeName: undefined,
  },
  rules: {
    categoryId: [
      { required: true, message: "请选择所属大类", trigger: "change" },
    ],
    typeName: [
      { required: true, message: "请输入设备类型名称", trigger: "blur" },
      { min: 0, max: 10, message: "设备类型名称过长", trigger: "blur" },
    ],
    notes: [
      {
        required: false,
        min: 0,
        max: 500,
        message: "备注不能超过500",
        trigger: "blur",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 提交按钮 */
const submitFormDebounceHandler = debounce(submitForm, 500);
function submitForm() {
  proxy.$refs["deviceTypeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updatedevicetype(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          reset();
          getList();
        });
      } else {
        adddevicetype(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          reset();
          getList();
        });
      }
    }
  });
}

function cancel() {
  open.value = false;
  reset();
}
function edit(data) {
  form.value = data;
  title.value = "编辑设备类型";
  open.value = true;
}
function reset() {
  form.value = {
    id: null,
    categoryId: undefined,
    typeName: undefined,
    notes: undefined,
  };
  proxy.resetForm("deviceTypeRef");
}
/** 查询岗位列表 */
function getList() {
  loading.value = true;
  devicetypeList(queryParams.value).then((response) => {
    console.log("222222222", response);
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  });
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    typeName: undefined,
  };
  getList();
}

function handleDelete(row) {
  console.log(row);

  proxy.$modal
    .confirm("是否确认删除该数据项？")
    .then(function () {
      return devicetypeDel([row.id]);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function addDeviceTypes() {
  title.value = "新增设备类型";
  open.value = true;
}

getList();
</script>
